package com.mall.page.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.mall.common.util.RespResult;
import com.mall.goods.feign.CategoryFeign;
import com.mall.goods.feign.SpuFeign;
import com.mall.goods.model.Category;
import com.mall.goods.model.Product;
import com.mall.goods.model.Sku;
import com.mall.goods.model.Spu;
import com.mall.page.web.service.IPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @author Darren
 * @date 2022-08-26 10:44
 * Description：
 */
@Service
public class PageServiceImpl implements IPageService {
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private CategoryFeign categoryFeign;

    @Value("${page-path}")
    private String pagePath;

    @Override
    public void generateHtml(String spuId) throws FileNotFoundException, UnsupportedEncodingException {
        // 1、创建Thymeleaf容器对象
        Context context = new Context();
        // 2、设置页面数据模型
        context.setVariables(dataLoad(spuId));
        // 3、创建文件
        File file = new File(pagePath, spuId + ".html");
        PrintWriter writer = new PrintWriter(file, "UTF-8");
        // 4、生成页面
        templateEngine.process("item", context, writer);
    }

    /**
     * 数据加载
     *
     * @param spuId
     * @return
     */
    private Map<String, Object> dataLoad(String spuId) {
        RespResult<Product> productResp = spuFeign.findBySpuId(spuId);
        Product product = productResp.getData();
        if (Objects.nonNull(product)) {
            // 数据模型
            Map<String, Object> dataMap = new HashMap<>();

            // Spu
            Spu spu = product.getSpu();
            dataMap.put("spu", spu);
            if (Objects.nonNull(spu)) {
                // 图片
                dataMap.put("images", spu.getImages().split(","));
                // 属性
                dataMap.put("attrs", JSON.parseObject(spu.getAttributeList()));
                // 三级分类
                RespResult<Category> oneCategoryResp = categoryFeign.getById(spu.getCategoryOneId());
                RespResult<Category> twoCategoryResp = categoryFeign.getById(spu.getCategoryTwoId());
                RespResult<Category> threeCategoryResp = categoryFeign.getById(spu.getCategoryThreeId());
                dataMap.put("oneCategory", oneCategoryResp.getData());
                dataMap.put("twoCategory", twoCategoryResp.getData());
                dataMap.put("threeCategory", threeCategoryResp.getData());
            }

            // Sku
            List<Sku> skus = product.getSkuList();
            if (Objects.nonNull(skus)) {
                List<Map<String, Object>> skuList = new ArrayList<>();
                for (Sku sku : skus) {
                    Map<String, Object> skuMap = new HashMap<>(4);
                    skuMap.put("id", sku.getId());
                    skuMap.put("name", sku.getName());
                    skuMap.put("price", sku.getPrice());
                    skuMap.put("attr", sku.getSkuAttribute());
                    skuList.add(skuMap);
                }
                dataMap.put("skuList", skuList);
            }

            return dataMap;
        }
        return null;
    }
}
